﻿namespace QLBanHang.UC
{
    using ConfigDatabase.Class;
    using DevExpress.Utils;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraTab;
    using BaseClass.Chung;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class UCdangnhap : XtraUserControl
    {
        private bool _bsystemname = false;
        private bool _bthanhcong = false;
        private ConnectDatabase _condata;
        private dtoDMDangNhap _dtodangnhap;
        private int _icount = 0;
        private string _pass = "";
        private string _quyensudung = "";
        private string _sSystemPass = ("admin" + DateTime.Now.ToString("yyMMddHHmm"));
        private string _sSystemsName = "administrator";
        private string _tennguoidung = "";
        private string _username = "";
        private SimpleButton btnketthuc;
        private SimpleButton btnluu;
        private IContainer components = null;
        private Label label2;
        private Label label3;
        private PictureEdit pictureEdit1;
        private TextEdit txtdangnhap;
        private TextEdit txtmatkhau;

        public UCdangnhap()
        {
            this.InitializeComponent();
            this._dtodangnhap = new dtoDMDangNhap();
        }

        private void btnketthuc_Click(object sender, EventArgs e)
        {
            try
            {
                XtraTabPage parent = (XtraTabPage) base.Parent;
                XtraTabControl control = (XtraTabControl) parent.Parent;
                control.TabPages.Remove(parent);
            }
            catch
            {
                ((Form) base.Parent).Close();
            }
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            this._bsystemname = false;
            this._bthanhcong = false;
            if (!this.f_kiemtraluu())
            {
                this._icount++;
            }
            else if ((this.txtdangnhap.Text == this._sSystemsName) && (this.txtmatkhau.Text == this._sSystemPass))
            {
                this._bsystemname = true;
                this._bthanhcong = true;
                this._tennguoidung = "Administrator";
                this._quyensudung = "";
                this.btnketthuc_Click(null, null);
            }
            else
            {
                this._dtodangnhap = new dtoDMDangNhap(this._condata);
                this._dtodangnhap.MATKHAU = this.txtmatkhau.Text;
                this._dtodangnhap.TENDANGNHAP = this.txtdangnhap.Text;
                this._dtodangnhap.Load();
                if (this._dtodangnhap.ID == 0)
                {
                    this._icount++;
                    MessageBox.Show("Th\x00f4ng tin đăng nhập kh\x00f4ng tồn tại.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    this.txtdangnhap.Focus();
                }
                else
                {
                    this._bthanhcong = true;
                    this._pass = this._dtodangnhap.MATKHAU;
                    this._username = this._dtodangnhap.TENDANGNHAP;
                    this._tennguoidung = this._dtodangnhap.TENNGUOIDUNG;
                    this._quyensudung = this._dtodangnhap.QUYENSUDUNG;
                    this.btnketthuc_Click(null, null);
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool f_kiemtraluu()
        {
            if (this.txtdangnhap.Text == "")
            {
                MessageBox.Show("Nhập t\x00ean đăng nhập.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.txtdangnhap.Focus();
                return false;
            }
            if (this._icount == 3)
            {
                return false;
            }
            return true;
        }

        private DataTable f_loaddata_nhomsudung()
        {
            DataTable table = new DataTable();
            table.Columns.Add("id", typeof(int));
            table.Columns.Add("ten");
            table.Rows.Add(new object[] { 1, "Quản trị hệ thống" });
            table.Rows.Add(new object[] { 2, "Người sử dụng" });
            return table;
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCdangnhap));
            this.btnketthuc = new DevExpress.XtraEditors.SimpleButton();
            this.btnluu = new DevExpress.XtraEditors.SimpleButton();
            this.label2 = new System.Windows.Forms.Label();
            this.txtdangnhap = new DevExpress.XtraEditors.TextEdit();
            this.label3 = new System.Windows.Forms.Label();
            this.txtmatkhau = new DevExpress.XtraEditors.TextEdit();
            this.pictureEdit1 = new DevExpress.XtraEditors.PictureEdit();
            ((System.ComponentModel.ISupportInitialize)(this.txtdangnhap.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtmatkhau.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureEdit1.Properties)).BeginInit();
            this.SuspendLayout();
            // 
            // btnketthuc
            // 
            this.btnketthuc.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.btnketthuc.Appearance.Font = new System.Drawing.Font("Tahoma", 10F);
            this.btnketthuc.Appearance.Options.UseFont = true;
            this.btnketthuc.Appearance.Options.UseTextOptions = true;
            this.btnketthuc.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
            this.btnketthuc.Image = ((System.Drawing.Image)(resources.GetObject("btnketthuc.Image")));
            this.btnketthuc.Location = new System.Drawing.Point(359, 88);
            this.btnketthuc.Name = "btnketthuc";
            this.btnketthuc.Size = new System.Drawing.Size(84, 30);
            this.btnketthuc.TabIndex = 6;
            this.btnketthuc.Text = " &Thoát";
            this.btnketthuc.Click += new System.EventHandler(this.btnketthuc_Click);
            // 
            // btnluu
            // 
            this.btnluu.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.btnluu.Appearance.Font = new System.Drawing.Font("Tahoma", 10F);
            this.btnluu.Appearance.Options.UseFont = true;
            this.btnluu.Appearance.Options.UseTextOptions = true;
            this.btnluu.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
            this.btnluu.Image = ((System.Drawing.Image)(resources.GetObject("btnluu.Image")));
            this.btnluu.Location = new System.Drawing.Point(245, 88);
            this.btnluu.Name = "btnluu";
            this.btnluu.Size = new System.Drawing.Size(108, 30);
            this.btnluu.TabIndex = 5;
            this.btnluu.Text = " &Đăng nhập";
            this.btnluu.Click += new System.EventHandler(this.btnluu_Click);
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Tahoma", 10F);
            this.label2.Location = new System.Drawing.Point(135, 21);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(113, 19);
            this.label2.TabIndex = 22;
            this.label2.Text = "Tên đăng nhập:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtdangnhap
            // 
            this.txtdangnhap.Location = new System.Drawing.Point(245, 20);
            this.txtdangnhap.Name = "txtdangnhap";
            this.txtdangnhap.Properties.MaxLength = 100;
            this.txtdangnhap.Size = new System.Drawing.Size(198, 20);
            this.txtdangnhap.TabIndex = 1;
            this.txtdangnhap.KeyDown += new System.Windows.Forms.KeyEventHandler(this.UCquanlydangnhap_edit_KeyDown);
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Tahoma", 10F);
            this.label3.Location = new System.Drawing.Point(135, 47);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(113, 19);
            this.label3.TabIndex = 22;
            this.label3.Text = "Mật khẩu:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtmatkhau
            // 
            this.txtmatkhau.Location = new System.Drawing.Point(245, 46);
            this.txtmatkhau.Name = "txtmatkhau";
            this.txtmatkhau.Properties.MaxLength = 3000;
            this.txtmatkhau.Properties.PasswordChar = '*';
            this.txtmatkhau.Size = new System.Drawing.Size(198, 20);
            this.txtmatkhau.TabIndex = 2;
            this.txtmatkhau.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtmatkhau_KeyDown);
            // 
            // pictureEdit1
            // 
            this.pictureEdit1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.pictureEdit1.EditValue = ((object)(resources.GetObject("pictureEdit1.EditValue")));
            this.pictureEdit1.Location = new System.Drawing.Point(3, 3);
            this.pictureEdit1.Name = "pictureEdit1";
            this.pictureEdit1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
            this.pictureEdit1.Size = new System.Drawing.Size(126, 115);
            this.pictureEdit1.TabIndex = 23;
            // 
            // UCdangnhap
            // 
            this.Controls.Add(this.pictureEdit1);
            this.Controls.Add(this.txtmatkhau);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txtdangnhap);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.btnluu);
            this.Controls.Add(this.btnketthuc);
            this.LookAndFeel.UseWindowsXPTheme = true;
            this.Name = "UCdangnhap";
            this.Size = new System.Drawing.Size(454, 125);
            this.Load += new System.EventHandler(this.UCphanquyensudung_Load);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.UCquanlydangnhap_edit_KeyDown);
            ((System.ComponentModel.ISupportInitialize)(this.txtdangnhap.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtmatkhau.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureEdit1.Properties)).EndInit();
            this.ResumeLayout(false);

        }

        private void txtmatkhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnluu_Click(null, null);
            }
        }

        private void txtmatkhau2_Validated(object sender, EventArgs e)
        {
        }

        private void UCphanquyensudung_Load(object sender, EventArgs e)
        {
            if (this._condata == null)
            {
                this._condata = new ConnectDatabase();
            }
        }

        private void UCquanlydangnhap_edit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        public ConnectDatabase pCondata
        {
            get
            {
                return this._condata;
            }
            set
            {
                this._condata = value;
            }
        }

        public dtoDMDangNhap pDangNhap
        {
            get
            {
                return this._dtodangnhap;
            }
            set
            {
                this._dtodangnhap = value;
            }
        }

        public string pMatKhau
        {
            get
            {
                return this._pass;
            }
            set
            {
                this._pass = value;
            }
        }

        public string pQuyenSuDung
        {
            get
            {
                return this._quyensudung;
            }
        }

        public bool pSystemsUser
        {
            get
            {
                return this._bsystemname;
            }
        }

        public string pTenDangNhap
        {
            get
            {
                return this._username;
            }
            set
            {
                this._username = value;
            }
        }

        public string pTenNguoiDung
        {
            get
            {
                return this._tennguoidung;
            }
            set
            {
                this._tennguoidung = value;
            }
        }

        public bool pThanhCong
        {
            get
            {
                return this._bthanhcong;
            }
        }
    }
}

